package com.hzncc.scaffold.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Major;
import com.hzncc.scaffold.entity.Other.Paper;
import com.hzncc.scaffold.entity.Other.Papermajor;
import com.hzncc.scaffold.entity.Other.Question;
import com.hzncc.scaffold.entity.Vo.CommitMyTestDetailVo;
import com.hzncc.scaffold.entity.Vo.MajorsVo;
import com.hzncc.scaffold.entity.Vo.MyTestDetailVo;
import com.hzncc.scaffold.entity.Vo.QuestionDetailVo;
import com.hzncc.scaffold.mapper.PaperMapper;
import com.hzncc.scaffold.service.MajorService;
import com.hzncc.scaffold.service.PaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzncc.scaffold.service.PapermajorService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.error.YAMLException;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 试卷表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Autowired
    PaperService paperService;

    @Autowired
    MajorService majorService;

    @Autowired
    PapermajorService papermajorService;

    @Override
    public ResultT savePaper(Paper paper) {
        boolean save = paperService.save(paper);
        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @Override
    public ResultT updatePaper(Paper paper) {
        boolean update = paperService.updateById(paper);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @Override
    public ResultT commit(CommitMyTestDetailVo commitMyTestDetailVo) {

        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Paper::getTitle,commitMyTestDetailVo.getTitle());
        lambdaQueryWrapper.eq(Paper::getType,commitMyTestDetailVo.getType());
        lambdaQueryWrapper.eq(Paper::getIsDelete,0);
        Paper one = paperService.getOne(lambdaQueryWrapper);
        if(BeanUtil.isNotEmpty(one)){
            throw new YAMLException("已存在同名试卷，不能重复添加");
        }
        Paper paper = BeanUtil.toBean(commitMyTestDetailVo, Paper.class);
        paper.setTheoryQuestion(commitMyTestDetailVo.getQuestion());
        paper.setCreateTime(DateUtil.now());

        if(StringUtils.isNotEmpty(commitMyTestDetailVo.getQuestion())){
            List<Integer> questionIdArr = new ArrayList<>();
            List<Question>  theoryQuestion = new ArrayList<>();
            List<Question>  operationQuestion = new ArrayList<>();
            Integer singleNum = 0;
            Integer multiNum = 0;
            Integer judgeNum = 0;
            Integer operationNum = 0;
            List<Question> questions = JSONUtil.toList(commitMyTestDetailVo.getQuestion(), Question.class);
            for (Question question : questions) {
                questionIdArr.add(question.getId());
                if(question.getType() == 3){
                    operationQuestion.add(question);
                    operationNum++;
                }else {
                    theoryQuestion.add(question);
                    if (question.getType() == 0) {
                        singleNum++;
                    }
                    else if (question.getType() == 1) {
                        multiNum++;
                    }
                    else if (question.getType() == 2) {
                        judgeNum++;
                    }
                }
            }
            paper.setTheoryQuestion(JSONUtil.toJsonStr(theoryQuestion));
            paper.setOperationQuestion(JSONUtil.toJsonStr(operationQuestion));
            paper.setSingleNum(singleNum);
            paper.setMultiNum(multiNum);
            paper.setJudgeNum(judgeNum);
            paper.setOperationNum(operationNum);
        }
        paper.setOperatorId(StpUtil.getLoginIdAsInt());
        boolean save = paperService.save(paper);
        List<Integer> majorIdList = commitMyTestDetailVo.getMajorId();
            for (Integer majorId : majorIdList) {
                Papermajor papermajor = new Papermajor();
                papermajor.setMajorId(majorId);
                papermajor.setPaperId(paper.getId());
                papermajorService.save(papermajor);
            }
        if (save) {
            return ResultT.success(paper.getId());
        }
        return ResultT.fail("操作失败");
    }

    @Override
    public MyTestDetailVo mytestDetail(Integer paperId) {

        Paper paper = paperService.getById(paperId);
        MyTestDetailVo myTestDetailVo = BeanUtil.toBean(paper, MyTestDetailVo.class);


        ArrayList<Major> majors = new ArrayList<>();

        LambdaQueryWrapper<Papermajor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Papermajor::getPaperId, paperId);
        List<Papermajor> papermajorList = papermajorService.list(lqw);

        for (Papermajor papermajor : papermajorList) {
            if (majorService.getById(papermajor.getMajorId()) != null) {
                Major major = majorService.getById(papermajor.getMajorId());
                majors.add(major);
            }
        }

        String theoryQuestion = paper.getTheoryQuestion();
        List<QuestionDetailVo> questionDetailVos = JSONUtil.toList(theoryQuestion,QuestionDetailVo.class);


        myTestDetailVo.setQuestion(questionDetailVos);
        myTestDetailVo.setMajors(majors);
        return myTestDetailVo;
    }
}
